package 剑指offer;

import javax.management.Query;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class p32_I从上到下打印二叉树 {
    public int[] levelOrder(TreeNode root) {
        if (root==null){
            return new int[]{};
        }

        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);

        //list 顺序表用来存储节点的值
        List<Integer> list=new ArrayList<>();

        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            if (poll.left!=null){
                queue.add(poll.left);
            }
            if (poll.right!=null) {
                queue.add(poll.right);
            }
            list.add(poll.val);
        }

        int[] res=new int[list.size()];
        for (int i = 0; i <list.size() ; i++) {
            res[i]=list.get(i);
        }

        return res;
    }
}
